package top.withwings.bonus.common.pojo;

import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import java.util.Map;

public class ReinforcedBeanPropertySqlParameterSource implements SqlParameterSource {

    private BeanPropertySqlParameterSource beanParameterSource;

    private MapSqlParameterSource mapParameterSource;


    public ReinforcedBeanPropertySqlParameterSource(Object bean, Map<String, ?> additionalParams) {

        this.beanParameterSource = new BeanPropertySqlParameterSource(bean);
        this.mapParameterSource = new MapSqlParameterSource(additionalParams);

    }

    @Override
    public boolean hasValue(String paramName) {

        return this.mapParameterSource.hasValue(paramName)
                || this.beanParameterSource.hasValue(paramName);
    }

    @Override
    public Object getValue(String paramName) throws IllegalArgumentException {
        if (this.mapParameterSource.hasValue(paramName)) {
            return this.mapParameterSource.getValue(paramName);
        } else {
            return this.beanParameterSource.getValue(paramName);
        }
    }

    @Override
    public int getSqlType(String paramName) {
        if(this.mapParameterSource.hasValue(paramName)){
            return this.mapParameterSource.getSqlType(paramName);
        }else{
            return this.beanParameterSource.getSqlType(paramName);
        }
    }

    @Override
    public String getTypeName(String paramName) {
        if(this.mapParameterSource.hasValue(paramName)){
            return this.mapParameterSource.getTypeName(paramName);
        }else{
            return this.beanParameterSource.getTypeName(paramName);
        }
    }


}